/************************************************************************************/
/*  Program name CAPAPIMP_SELECT_INV_CHECKS.sql                                     */
/*  Creater:  WONG Wing Kin (PCCW - UniHub)                                         */
/*************   History   *************************                                */
/* 30 NOV, 2005   WONG Wing Kin         First created                               */
/************************************************************************************/
SET SERVEROUTPUT ON SIZE 1000000;

DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPAPIMP_SELECT_INV_CHECKS';

	lv_Scc_Count	NUMBER			:=	0;
	lv_Err_Count	NUMBER			:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	CURSOR Cur_F IS
	SELECT
		*
	FROM
		CRST_AP_SELECT_INV_CHECKS;

	lv_Next_Seq_ID		NUMBER;
	lv_Vend_ID			NUMBER;

BEGIN

	FOR C1 IN Cur_F LOOP
		BEGIN
			SELECT	AP_SELECTED_INVOICE_CHECKS_S.NextVal
			INTO	lv_Next_Seq_ID
			FROM	DUAL;

			INSERT INTO CRST_AP_SELECT_INV_CHECKS_MAP VALUES
			(
			C1.SELECTED_CHECK_ID,
			lv_Next_Seq_ID
			);

			lv_Vend_ID	:=	CAPBRIMP_COMMON.GetVendorID_Name(C1.VENDOR_NAME);

			INSERT INTO AP_SELECTED_INVOICE_CHECKS_ALL
			VALUES
			(
			C1.CHECKRUN_NAME,
			C1.CHECK_NUMBER,
			lv_Vend_ID,												--VENDOR_ID,
			CAPBRIMP_COMMON.GetVendorSiteID(lv_Vend_ID, C1.ORG_ID, C1.VENDOR_SITE_CODE),	--VENDOR_SITE_ID,
			C1.PRINT_SEQUENCE_NUM,
			C1.CHECK_AMOUNT,
			C1.VENDOR_AMOUNT,
			C1.LAST_UPDATE_DATE,
			CAPBRIMP_COMMON.GetUser(C1.LAST_UPDATED_BY_NAME),		--LAST_UPDATED_BY,
			C1.OK_TO_PAY_FLAG,
			C1.DONT_PAY_REASON_CODE,
			C1.STATUS_LOOKUP_CODE,
			C1.VENDOR_NUM,
			C1.VENDOR_NAME,
			C1.VENDOR_SITE_CODE,
			C1.CUSTOMER_NUM,
			C1.ADDRESS_LINE1,
			C1.ADDRESS_LINE2,
			C1.ADDRESS_LINE3,
			C1.CITY,
			C1.STATE,
			C1.ZIP,
			C1.PROVINCE,
			C1.COUNTRY,
			C1.WITHHOLDING_STATUS_LOOKUP_CODE,
			C1.VOID_FLAG,
			CAPBRIMP_MAPPING.GetCheckIDMapping(C1.CHECK_ID),		--CHECK_ID
			C1.CURRENCY_CODE,
			C1.DISCOUNT_AMOUNT,
			C1.BANK_ACCOUNT_NUM,
			C1.BANK_NUM,
			C1.BANK_ACCOUNT_TYPE,
			C1.CHECK_VOUCHER_NUM,
			C1.LAST_UPDATE_LOGIN,
			C1.CREATION_DATE,
			CAPBRIMP_COMMON.GetUser(C1.CREATED_BY_NAME),			--CREATED_BY,
			C1.FUTURE_PAY_DUE_DATE,
			C1.DUE_DATE,
			C1.PAYMENT_DATE,
			lv_Next_Seq_ID,											--SELECTED_CHECK_ID,
			C1.ATTENTION_AR_FLAG,
			CAPBRIMP_COMMON.GetOrgID(C1.ORG_NAME),					--ORG_ID,
			C1.TRANSFER_PRIORITY,
			CAPBRIMP_MAPPING.GetBankACMapping(C1.EXTERNAL_BANK_ACCOUNT_ID),	--EXTERNAL_BANK_ACCOUNT_ID,
			C1.ADDRESS_LINE4,
			C1.IBAN_NUMBER
			);
		EXCEPTION
			WHEN OTHERS THEN
				DBMS_OUTPUT.Put_Line(SQLERRM);
				lv_Err_Count	:=	lv_Err_Count + 1;
		END;
		lv_Scc_Count	:=	lv_Scc_Count	+	1;
	END LOOP;

	IF	lv_Err_Count	>	0	THEN
		ROLLBACK;
	END IF;

	INSERT INTO CAPCNV_LOG(
		PROG_ID,
		PROCESS_DATE,
		RECORD_PROCESSED,
		RECORD_REJECTED,
		RECORD_EXTRACTED,
		ERR_MSG
	) VALUES (
		lv_Prog_ID,
		SYSDATE,
		lv_Scc_Count,
		lv_Err_Count,
		lv_Scc_Count-lv_Err_Count,
		lv_Err_Msg
	);

	COMMIT;
END;
/
